package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.ysd.dao.impl.Produc2tDaoImpl;
import com.ysd.entity.Product;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;


public class AdminProductDetailsCategoryServlet extends BaseServlet {
	/**
	 *查询所有产品分类详情
	 *localhost:8080/kouhong/adminProductDetailsCategory?reqName=getProductDetailsCate
	 */
	public void getProductDetailsCate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int productId=0;
		if(request.getParameter("productId")!=null && !request.getParameter("productId").equals("")) {
			productId =Integer.parseInt(request.getParameter("productId"));
		}
		
		
		int beginProductPrice= 0;
		if(request.getParameter("beginProductPrice")!=null && !request.getParameter("beginProductPrice").equals("")) {
			beginProductPrice=Integer.parseInt(request.getParameter("beginProductPrice"));//NumberFormatException数字格式化异常
		}
		
		int endProductPrice= 0 ;
		if(request.getParameter("endProductPrice")!=null && !request.getParameter("endProductPrice").equals("")) {
			endProductPrice =Integer.parseInt(request.getParameter("endProductPrice"));	
		}
		String productName = request.getParameter("productName");
		String brand = request.getParameter("brand");
		String productColor = request.getParameter("productColor");
		
		int productCategoryId=0;
		if(request.getParameter("productCategoryId")!=null && !request.getParameter("productCategoryId").equals("")) {
			 productCategoryId =Integer.parseInt(request.getParameter("productCategoryId"));	
		}
		
		String imgs = request.getParameter("imgs");
		String getMetails = request.getParameter("getMetails");
		
		Product p = new Product(productId, productName, 0, 0, beginProductPrice, endProductPrice, brand, productColor, productCategoryId, imgs, getMetails, "");
		Produc2tDaoImpl pDao = new Produc2tDaoImpl();
		int page  = 0;
		if(request.getParameter("page")!=null&& !request.getParameter("page").equals("")) {
			 page  = Integer.parseInt(request.getParameter("page"));
		}
		int size  =0;
		if(request.getParameter("size") !=null && !request.getParameter("size").equals("")) {
			size  = Integer.parseInt(request.getParameter("size"));
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		Gson gson = new Gson();
		
		List<Product> pList = null;
		if(page==0) {
			 
			 int counts =  pDao.getProductCount(p);
			 String count = ""+counts;
			 System.out.println("count="+count);
			 out.print(count);
		}else {
			 pList = pDao.getProductList(p,page,size);
			 String res = gson.toJson(pList);
			 out.print(res);
		}
		
		

		
		out.flush();
		out.close();
	}

	/**
	 *通过产品Id删除对应的产品
	 *localhost:8080/kouhong/adminProductDetailsCategory?reqName=getProductDetailsCategorys
	 */
	public void getProductDetailsCategorys(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Produc2tDaoImpl pDao = new Produc2tDaoImpl();
		int dpId = Integer.parseInt(request.getParameter("productId"));

		int n = pDao.deleteProductDetailsCategoryId(dpId);
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		if (n>0) {
			out.print(Result.toClient(true, "删除成功", ""));
		} else {
			out.print(Result.toClient(false, "删除失败", ""));
		}
		
		
		out.flush();
		out.close();
	}
	
	/**
	 *localhost:8080/kouhong/adminProductDetailsCategory?reqName=addProductDetailsCategorys
	 *添加产品
	 */
	public void addProductDetailsCategorys(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String productName = request.getParameter("productName");
		int productId = 0;
		if(request.getParameter("productId")!=null && !request.getParameter("productId").equals("")) {
			productId = Integer.parseInt(request.getParameter("productId"));
		}
		
		int productPrice = 0; 
		if(request.getParameter("productPrice")!=null && !request.getParameter("productPrice").equals("")) {
			productPrice = Integer.parseInt(request.getParameter("productPrice"));
		}
		int salePrice=0;
		if(request.getParameter("salePrice")!=null && !request.getParameter("salePrice").equals("")) {
			 salePrice = Integer.parseInt(request.getParameter("salePrice"));
		}
		String brand = request.getParameter("brand");
		String productColor = request.getParameter("productColor");
		int productCategoryId = 0;
		if(request.getParameter("productCategoryId")!=null && !request.getParameter("productCategoryId").equals("")) {
			productCategoryId = Integer.parseInt(request.getParameter("productCategoryId"));
		}
		String imgs = request.getParameter("imgs");
		String getMetails = request.getParameter("getMetails");
		Product p = new Product(productId, productName, productPrice, salePrice, brand, productColor, productCategoryId, imgs, getMetails);
		Produc2tDaoImpl pDao = new Produc2tDaoImpl();
		int n = pDao.addProduct(p);
		

		if (n>0) {
			out.print(Result.toClient(true, "添加成功", ""));
		} else {
			out.print(Result.toClient(false, "添加失败", ""));
		}
		
		out.flush();
		out.close();
	}
/**
 *localhost:8080/kouhong/adminProductDetailsCategory?reqName=uploadImg
 * @param request
 * @param response
 * @throws ServletException
 * @throws IOException
 */
	public void uploadImg(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		// 设置编码格式
		PrintWriter out = response.getWriter(); 
		// 1.创建SmartUpload
		SmartUpload smart = new SmartUpload(); 
		// 2.设置字符集
		smart.setCharset("UTF-8"); 
		// 3.初始化
		smart.initialize(getServletConfig(), request, response);
		
		try { // 4.上传文件 smart.upload(); // 5.保存文件到指定位置(save只提供路径，使用原文件名上传文件)
				// 
			smart.upload();
			// 5.保存文件到指定位置(saveAs可以用指定名称上传文件) //
				// 
			String filename= System.currentTimeMillis()+".jpg";
			smart.getFiles().getFile(0).saveAs("/uploadImg/product/"+filename);
				// // 获取文件名
			filename = "/uploadImg/product/"+filename;
			out.print(Result.toClient(true, "上传成功",filename));
		} catch (SmartUploadException e) {
			out.print(Result.toClient(false, "上传失败", "图片过大"));
			e.printStackTrace();
		}
		out.flush();
		out.close();
	}
	
	/**
	 *localhost:8080/kouhong/adminProductDetailsCategory?reqName=getProductDetailsCateById
	 *添加产品详情
	 */
	public void getProductDetailsCateById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		Produc2tDaoImpl pcDao = new Produc2tDaoImpl();
		int pcId =0;
		if(request.getParameter("productId")!=null && !request.getParameter("productId").equals("")) {
			pcId = Integer.parseInt(request.getParameter("productId"));
			System.err.println(pcId);
		}
		Product p = pcDao.getProductId(pcId);
		
		Gson gson = new Gson();
		String res = gson.toJson(p);
		out.print(res);
	
		out.flush();
		out.close();
	}
	
	
	
	/**
	 *localhost:8080/kouhong/adminProductDetailsCategory?reqName=updateProductDetailsCate
	 *修改产品详情
	 */
	public void updateProductDetailsCate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		Produc2tDaoImpl pcDao = new Produc2tDaoImpl();
		int productId = 0;
		if(request.getParameter("productId")!=null && !request.getParameter("productId").equals("")) {
			productId = Integer.parseInt(request.getParameter("productId"));
		}
		String  productName = request.getParameter("productName");
		int productPrice = 0;
		if(request.getParameter("productPrice")!=null && !request.getParameter("productPrice").equals("")) {
			productPrice = Integer.parseInt(request.getParameter("productPrice"));
		}
		int salePrice = 0;
		if(request.getParameter("salePrice")!=null && !request.getParameter("salePrice").equals("")) {
			salePrice = Integer.parseInt(request.getParameter("salePrice"));
		}
		String brand = request.getParameter("brand");
		String productColor = request.getParameter("productColor");
		int productCategoryId = 0;
		if(request.getParameter("productCategoryId")!=null && !request.getParameter("productCategoryId").equals("")) {
			productCategoryId = Integer.parseInt(request.getParameter("productCategoryId"));
		}
		String imgs = request.getParameter("imgs");
		String getMetails = request.getParameter("getMetails");
		Product p = new Product(productId, productName, productPrice, salePrice, brand, productColor, productCategoryId, imgs, getMetails);
		int n = pcDao.updateProducts(p);
	
		if (n>0) {
			out.print(Result.toClient(true, "修改成功", ""));
		} else {
			out.print(Result.toClient(false, "修改失败", ""));
		}
		out.flush();
		out.close();
	}
	
	/**
	 *通过产品Id批量删除对应的产品
	 *localhost:8080/kouhong/adminProductDetailsCategory?reqName=deleteProductDetaById
	 */
	public void deleteProductDetaById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Produc2tDaoImpl pDao = new Produc2tDaoImpl();
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String prId = request.getParameter("productId");
		int n = pDao.deleteProId(prId);

		if (n>0) {
			out.print(Result.toClient(true, "删除成功", ""));
		} else {
			out.print(Result.toClient(false, "删除失败", ""));
		}
		
		
		out.flush();
		out.close();
	}
	
}
